/*
 * FeedRestController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.onesalt.domain.Feed;
import com.onesalt.domain.Order;
import com.onesalt.domain.User;
import com.onesalt.service.FeedService;
import com.onesalt.service.OrderService;
import com.onesalt.web.api.request.PageRequest;
import com.onesalt.web.api.response.FeedListResponse;

/**
 * 精选
 *
 * @author kiri
 * @date 2015年4月19日
 * @version 1.0
 */
@RestController
@RequestMapping("/api/feeds")
public class FeedRestController extends AbstractRestController {

	@Autowired
	private OrderService orderService;
	
	@Autowired
	private FeedService feedService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public FeedListResponse findAll(@ModelAttribute PageRequest request) {
		User user = getValidUser(request);
		List<Feed> feeds = feedService.getFeedRepository().findByStatusOrderByViewPositionAsc(Feed.STATUS_VALID);
		Order remindedOrder = null;
		if (request.getPage() == 0) {
			remindedOrder = orderService.findRemindedOrder(user.getUserId());
		}
		
		FeedListResponse resp = new FeedListResponse();
		resp.setFeeds(feeds);
		resp.setRemindedOrder(remindedOrder);
		return resp;
	}
}
